package info_serv

import (
	"strconv"
	"strings"
	"ubt-gin-admin/app/api/model/info_mod"
	"ubt-gin-admin/app/pkg/db"
)

type infoKw struct{}

var KeywordServ = infoKw{}

// Add 新增配置
func (s *infoKw) Add(req *info_mod.KeywordSaveReq) (string, string) {
	sqlStr := "INSERT INTO info_keyword(kw_name, kw_count, kind, states, add_time, upd_time) VALUES ($1, $2, 2, 1, EXTRACT(epoch FROM NOW()), EXTRACT(epoch FROM NOW()))"
	return db.CudData(sqlStr, req.KwName, req.KwCount)
}

// Exists 判断是否存在
func (s *infoKw) Exists(req *info_mod.KeywordExtVo) bool {
	sqlStr := "SELECT kw_id FROM info_keyword WHERE states=1 AND kind=2 AND kw_name=$1"
	param := make([]any, 0)
	param = append(param, req.KwName)
	if req.KwId != "" {
		sqlStr += " AND kw_id!=$2"
		param = append(param, req.KwId)
	}
	if _, _, data := db.GetOne(sqlStr, param...); len(data) == 0 {
		return false
	}
	return true
}

// Update 修改配置
func (s *infoKw) Update(req *info_mod.KeywordSaveReq) (string, string) {
	sqlStr := "UPDATE info_keyword SET kw_name=$1, kw_count=$2, upd_time=EXTRACT(epoch FROM NOW()) WHERE kw_id=$3"
	return db.CudData(sqlStr, req.KwName, req.KwCount, req.KwId)
}

// Process 处理配置
func (s *infoKw) Process(kindIds string) (string, string) {
	var (
		param []any
		q     string
		n     = 0
	)
	idList := strings.Split(kindIds, ",")
	for _, curId := range idList {
		n++
		q += "$" + strconv.Itoa(n) + ","
		param = append(param, curId)
	}
	q = q[:len(q)-1]
	sqlStr := "UPDATE info_keyword SET states=2, upd_time=EXTRACT(epoch FROM NOW()) WHERE kw_id IN (" + q + ")"
	return db.CudData(sqlStr, param...)
}

// One 获取配置详情
func (s *infoKw) One(kwId string) (string, string, any) {
	sqlStr := "SELECT kw_id, kw_name, kw_count, kind, states, add_time, upd_time FROM info_keyword WHERE kw_id=$1"
	return db.GetOne(sqlStr, kwId)
}

// Count 获取配置数量
func (s *infoKw) Count(req *info_mod.KeywordPageReq) (string, string, any) {
	sqlStr := "SELECT COUNT(1) AS nums FROM info_keyword WHERE states=1 AND kind=2"
	param := make([]any, 0)
	var n = 0
	if req.Keywords != "" {
		sqlStr += " AND ("
		nameArr := strings.Split(req.Keywords, " ")
		for _, name := range nameArr {
			sqlStr += "kw_name LIKE '%'||$" + strconv.Itoa(n+1) + "||'%' OR "
			param = append(param, name)
			n++
		}
		sqlStr = sqlStr[:len(sqlStr)-4]
		sqlStr += ")"
	}
	return db.GetOne(sqlStr, param...)
}

// Page 获取配置分页列表
func (s *infoKw) Page(req *info_mod.KeywordPageReq) (string, string, any) {
	sqlStr := "SELECT kw_id, kw_name, kw_count, kind FROM info_keyword WHERE states IN (1, 3)"
	param := make([]any, 0)
	var n = 0
	if req.Keywords != "" {
		sqlStr += " AND ("
		nameArr := strings.Split(req.Keywords, " ")
		for _, name := range nameArr {
			sqlStr += "kw_name LIKE '%'||$" + strconv.Itoa(n+1) + "||'%' OR "
			param = append(param, name)
			n++
		}
		sqlStr = sqlStr[:len(sqlStr)-4]
		sqlStr += ")"
	}
	sqlStr += " ORDER BY upd_time DESC LIMIT $" + strconv.Itoa(n+1) + " OFFSET $" + strconv.Itoa(n+2)
	param = append(param, req.Limit)
	param = append(param, req.Offset)
	return db.GetAll(sqlStr, param...)
}

// Select 获取配置下拉列表
func (s *infoKw) Select(req *info_mod.KeywordSelReq) (string, string, any) {
	sqlStr := "SELECT kw_id, kw_name, kw_count, kind FROM info_keyword WHERE states=1"
	param := make([]any, 0)
	var n = 0
	if req.Kind != 0 {
		sqlStr += " AND kind=$" + strconv.Itoa(n+1)
		param = append(param, req.Kind)
		n++
	}
	if req.KwUser != "" {
		sqlStr += " AND kw_user=$'" + strconv.Itoa(n+1) + "'"
		param = append(param, req.KwUser)
		n++
	}
	return db.GetAll(sqlStr, param...)
}
